﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("攻击补偿速度")]
    public float lightSpeed; // 轻攻击速度
    public float heavySpeed; // 重攻击速度

    [Header("打击感")]
    public float shakeTime; // 摇晃时间
    public int lightPause; // 轻攻击暂停时间
    public float lightStrength; // 轻攻击相机震动强度
    public int heavyPause; // 重攻击暂停时间
    public float heavyStrength; // 重攻击相机震动强度

    [Header("攻击")]
    public float interval = 2f; // 攻击间隔时间
    private float timer; // 计时器
    private bool isAttack; // 是否正在攻击
    private string attackType; // 攻击类型
    private int comboStep; // 连击步骤

    [Header("移动和跳跃参数")] 
    public float moveSpeed; // 移动速度
    public float jumpForce; // 跳跃力量
    new private Rigidbody2D rigidbody; // 刚体组件
    private Animator animator; // 动画控制器
    private float input; // 输入
    private bool isGround; // 是否在地面上
    [SerializeField] private LayerMask layer; // 地面碰撞层

    [SerializeField] private Vector3 check; // 地面检测向量

    void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        input = Input.GetAxisRaw("Horizontal");
        isGround = Physics2D.OverlapCircle(transform.position + new Vector3(check.x, check.y, 0), check.z, layer);

        animator.SetFloat("Horizontal", rigidbody.velocity.x);
        animator.SetFloat("Vertical", rigidbody.velocity.y);
        animator.SetBool("isGround", isGround);

        Move();
        Attack();
    }

    void Move()
    {
        // 如果玩家没有在攻击状态下，根据输入来移动角色
        if (!isAttack)
            rigidbody.velocity = new Vector2(input * moveSpeed, rigidbody.velocity.y);
        else
        {
            // 如果正在攻击，则根据攻击类型设置速度
            if (attackType == "Light")
                rigidbody.velocity = new Vector2(transform.localScale.x * lightSpeed, rigidbody.velocity.y);
            else if (attackType == "Heavy")
                rigidbody.velocity = new Vector2(transform.localScale.x * heavySpeed, rigidbody.velocity.y);
        }

        // 处理跳跃
        if (Input.GetButtonDown("Jump") && isGround)
        {
            rigidbody.velocity = new Vector2(0, jumpForce);
            animator.SetTrigger("Jump"); // 触发跳跃动画
        }

        // 根据水平速度方向更新角色朝向
        if (rigidbody.velocity.x < 0)
            transform.localScale = new Vector3(-1, 1, 1); // 向左
        else if (rigidbody.velocity.x > 0)
            transform.localScale = new Vector3(1, 1, 1); // 向右
    }

    void Attack()
    {
        // 轻攻击输入检测
        if (Input.GetKeyDown(KeyCode.Return) && !isAttack)
        {
            isAttack = true;
            attackType = "Light";
            comboStep++; // 连击步骤加一
            if (comboStep > 3)
                comboStep = 1; // 连击步骤循环
            timer = interval; // 设置计时器
            animator.SetTrigger("LightAttack"); // 触发轻攻击动画
            animator.SetInteger("ComboStep", comboStep); // 设置连击步骤参数
        }
        // 重攻击输入检测
        if (Input.GetKeyDown(KeyCode.RightShift) && !isAttack)
        {
            isAttack = true;
            attackType = "Heavy";
            comboStep++; // 连击步骤加一
            if (comboStep > 3)
                comboStep = 1; // 连击步骤循环
            timer = interval; // 设置计时器
            animator.SetTrigger("HeavyAttack"); // 触发重攻击动画
            animator.SetInteger("ComboStep", comboStep); // 设置连击步骤参数
        }

        // 处理连击计时器
        if (timer != 0)
        {
            timer -= Time.deltaTime;
            if (timer <= 0)
            {
                timer = 0;
                comboStep = 0; // 重置连击步骤
            }
        }
    }

    //攻击结束
    public void AttackOver()
    {
        isAttack = false;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Enemy"))
        {
            // 根据攻击类型处理打击感
            if (attackType == "Light")
            {
                AttackSense.Instance.HitPause(lightPause); // 轻攻击暂停
                AttackSense.Instance.CameraShake(shakeTime, lightStrength); // 相机震动
            }
            else if (attackType == "Heavy")
            {
                AttackSense.Instance.HitPause(heavyPause); // 重攻击暂停
                AttackSense.Instance.CameraShake(shakeTime, heavyStrength); // 相机震动
            }

            // 根据角色朝向确定敌人受击方向
            if (transform.localScale.x > 0)
                other.GetComponent<Enemy>().GetHit(Vector2.right); // 右侧受击
            else if (transform.localScale.x < 0)
                other.GetComponent<Enemy>().GetHit(Vector2.left); // 左侧受击
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(transform.position + new Vector3(check.x, check.y, 0), check.z);
    }

}